package com.runen.infinitemovement.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.runen.infinitemovement.R;
import com.runen.infinitemovement.adapter.MyMedalTypeNameAdapter;
import com.runen.infinitemovement.base.BaseActivity;
import com.runen.infinitemovement.fragment.MyMedalFragment;
import com.runen.infinitemovement.tool.CommonUtil;
import com.runen.infinitemovement.tool.notchtool.NotchTools;
import com.runen.infinitemovement.tool.notchtool.core.NotchProperty;
import com.runen.infinitemovement.tool.notchtool.core.OnNotchCallBack;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MyMedalActivity extends BaseActivity {

    @BindView(R.id.btn_finish)
    Button btnFinish;
    @BindView(R.id.ll_title)
    LinearLayout ll_title;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    @BindView(R.id.rv_title)
    RecyclerView rvTitle;
    private String[] sportsTypeNames = new String[]{"红行长征", "运动征途", "活动勋章"};
    private List<String> typeList = new ArrayList<>();
    MyMedalTypeNameAdapter myMedalTypeNameAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_medal;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        ButterKnife.bind(this);
        //适配刘海屏
        NotchTools.getFullScreenTools().translucentStatusBar(this, new OnNotchCallBack() {
            @Override
            public void onNotchPropertyCallback(NotchProperty notchProperty) {
                RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) ll_title.getLayoutParams();
                layoutParams.topMargin = CommonUtil.getStatusBarHeight(MyMedalActivity.this);
                ll_title.setLayoutParams(layoutParams);
            }
        });
        LinearLayoutManager layoutManager = new GridLayoutManager(this, 3);
        rvTitle.setLayoutManager(layoutManager);
        myMedalTypeNameAdapter = new MyMedalTypeNameAdapter(this);
        rvTitle.setAdapter(myMedalTypeNameAdapter);
        typeList.clear();
        for (int i = 0; i < sportsTypeNames.length; i++) {
            typeList.add(sportsTypeNames[i]);
        }
        myMedalTypeNameAdapter.setNewData(typeList);
        initTypeName();
        myMedalTypeNameAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                myMedalTypeNameAdapter.setCheckPosition(position);
                myMedalTypeNameAdapter.notifyDataSetChanged();
                viewPager.setCurrentItem(position);
            }
        });
    }

    private void initTypeName() {
        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                MyMedalFragment medalFragment = (MyMedalFragment) MyMedalFragment.newInstance(position);
                return medalFragment;
            }

            @Override
            public int getCount() {
                return typeList.size();
            }
        });
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                myMedalTypeNameAdapter.setCheckPosition(position);
                myMedalTypeNameAdapter.notifyDataSetChanged();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    protected boolean isUseFullScreenMode() {
        return true;
    }

    @OnClick(R.id.btn_finish)
    public void onClick() {
        finish();
    }

}